﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovedSweet : MonoBehaviour
{

    private gameSweet sweet;
    private IEnumerator moveCoroutine;
    void Awake()
    {
        sweet = GetComponent<gameSweet>();
    }

    public void Move( int newX,int newY ,float time)
    {
        if (moveCoroutine != null)
        {
            StopCoroutine(moveCoroutine);
        }

        moveCoroutine = MoveCoroutine(newX, newY, time);
        StartCoroutine(moveCoroutine);
    }

    private IEnumerator MoveCoroutine(int newX,int newY,float time)
    {
        sweet.X = newX;
        sweet.Y = newY;
        Vector2 startPos = transform.position;
        Vector2 endPos = sweet.gameManager.CorrectPosition(newX, newY);

        for (float i = 0; i < time; i+=Time.deltaTime)
        {
            sweet.transform.position = Vector2.Lerp(startPos, endPos, i / time);
            yield return 0;
        }

        sweet.transform.position = endPos;
    }
}
